﻿/*
 * Создан: Оникийчук Антон Игоревич
 * Компания: ООО "ИМСАТ"
 * Дата создания: 29.01.2009
 * Время создания: 14:59
 * Emal разработчика: onikiychuka@gmail.com
 */
using System.Xml;

namespace IMSAT.OFTD.DOM
{
    /// <summary>
    /// Description of Link.
    /// </summary>
    public class Link : Node
    {
        private string _firstConnectionPointName = "";
        private Element _firstItem;
        private string _fitId = "";
        private string _secondConnectionPointName = "";
        private Element _secondItem;
        private string _sitId = "";

        public Element FirstItem
        {
            get { return _firstItem; }
        }

        public string FirstConnectionPointName
        {
            get { return _firstConnectionPointName; }
        }

        public Element SecondItem
        {
            get { return _secondItem; }
        }

        public string SecondConnectionPointName
        {
            get { return _secondConnectionPointName; }
        }

        internal void SetelemsByID(Page pg)
        {
            
        	CheckNodeDelegateChain delchain1  = new CheckNodeDelegateChain();
        	delchain1.AddDelegate(delegate(Node obj)
                                                   {
                                                       if (obj is Element && (obj as Element).Id.OriginalId == _fitId)
                                                           return true;
                                                       return false;
                                                   });
        	
        	
        	delchain1.Finish();
        	NodeCollection<Node> res = pg.Find(delchain1);
        	CheckNodeDelegateChain delchain2  = new CheckNodeDelegateChain();
        	delchain2.AddDelegate(delegate(Node obj)
                                                    {
                                                        if (obj is Element && (obj as Element).Id.OriginalId == _sitId)
                                                            return true;
                                                        return false;
                                                    });
            delchain2.Finish();
            NodeCollection<Node> res1 = pg.Find(delchain2);
            if (res.Count == 1)
                _firstItem = res[0] as Element;
            if (res1.Count == 1)
                _secondItem = res1[0] as Element;
        }

        #region Nested type: Reader

        public class Reader : NodeXmlReader
        {
            protected override bool CheckReader(XmlReader rd, bool _prevRes, Node parentNode)
            {
                if (_prevRes)
                {
                    if (rd.LocalName == "Связь")
                        return true;
                    return false;
                }
                return base.CheckReader(rd, _prevRes, parentNode);
            }

            private string[] split(string data)
            {
                string[] res = data.Split('/');
                res[0] = res[0].Substring(res[0].IndexOf("(") + 1, res[0].IndexOf(")") - (res[0].IndexOf("(") + 1));
                return res;
            }

            protected override Node InitNode(XmlReader reader, Node initedNode)
            {
                if (initedNode == null)
                    initedNode = new Link();
                var l = (Link) initedNode;
                string t1 = reader.GetAttribute("т1");
                string t2 = reader.GetAttribute("т2");
                string[] res1 = split(t1);
                l._firstConnectionPointName = res1[1];
                l._fitId = res1[0];
                res1 = split(t2);
                l._secondConnectionPointName = res1[1];
                l._sitId = res1[0];
                return base.InitNode(reader, initedNode);
            }

            protected override Node InitObjectAfterBuild(Node result)
            {
                return base.InitObjectAfterBuild(result);
            }
        }

        #endregion
    }
}